/*
 * =====================================================================================
 *
 *       Filename:  common.h
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  03/06/19 15:59:04
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  liushuyong (), shuyong1999@163.com
 *        Company:  shuyong
 *
 * =====================================================================================
 */

#ifndef  CommHead_H_
#define  CommHead_H_

#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <uuid/uuid.h>

#include "boost/shared_ptr.hpp"
#include "boost/make_shared.hpp"
#include "boost/atomic.hpp"
#include "boost/format.hpp"
#include "boost/locale.hpp"
#include <boost/intrusive_ptr.hpp>  // boost::intrusive_ptr
#include <boost/unordered_set.hpp>
#include "boost/unordered_map.hpp"
#include "boost/algorithm/string.hpp"
#include <boost/xpressive/detail/utility/ignore_unused.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>

//brpc
#include <brpc/socket.h>
#include <brpc/shared_object.h>
#include <brpc/channel_base.h>  // ChannelBase
#include <brpc/excluded_servers.h>  // ExcludedServers
#include <brpc/load_balancer.h>
#include <brpc/server.h>
#include <brpc/channel.h>
#include <brpc/restful.h>
#include <brpc/policy/hasher.h>
#include <butil/strings/string_number_conversions.h>
#include <butil/iobuf.h>
#include <gflags/gflags.h>
#include "bvar/bvar.h"
#include "bthread/bthread.h"
#include "bthread/butex.h"
#include "bthread/unstable.h"

//glog
#include "glog/logging.h"

#include <butil/logging.h>
#include "json2pb/pb_to_json.h"
#include "json2pb/json_to_pb.h"
#include "cjson/cJSON.h"
const static char* VERSION = "";

enum {
    FEMALE = 0,
    MALE,
};

enum {
    TAOTAO = 0,
    BINBIN,
    SAINAN,
    TINGTING,

};

//引擎参数 key
enum {
    TEXT_ENCODE_TYPE_INDEX = 0,
    LANGUAGE_INDEX = 1,
    TIMBRE_INDEX = 2,
    PITCH_INDEX = 3,
    VOLUME_INDEX = 4,
    SPEED_INDEX = 5,
    SAMPLE_RATE_INDEX = 6,
    RD_PUNC_MODE_INDEX = 7,
    ENTER_RETURN_MODE_INDEX = 8,
    TEXT_TYPE_INDEX = 9,
    EXTRACTOR_INDEX = 10,
    SUPPORT_PARAMETERS_NUM,
};

//0        1       2      3     4      5    6      7       8     9      10
const static char* ENGINE_PARAMETERS_KEY[SUPPORT_PARAMETERS_NUM] = { "tte", "lang", "tim", "pit", "vol", "sp", "sr", "rpm", "erm", "tt", "aue" };

//
const static double ENGINE_PARAMETERS_MIN[SUPPORT_PARAMETERS_NUM] = { 0, 0, TAOTAO, 0, 0.1, 0.5, 4000, 0, 0, 0, 0 };

const static double ENGINE_PARAMETERS_MAX[SUPPORT_PARAMETERS_NUM] = { 1, 4, TINGTING, 10, 10.0, 2.0, 24000, 1, 1, 1, 3 };

const static double ENGINE_PARAMETERS_DEFAULT[SUPPORT_PARAMETERS_NUM] = { 1, 0, TAOTAO, 0, 2.0, 1.0, 24000, 0, 0, 0, 0 };

//错误码
const int OK = 0;
const int ERROR_PARAMETER = 11001;
const int ERROR_REQUEST_CNTL = 30201;
const int ERROR_SERVER_BUSY = 30202;
const int ERROR_TEXT_TOO_SHORT = 30203;
const int ERROR_TEXT_TOO_LONG = 30204;
const int ERROR_SEQID_FOR_SESSION = 30205;
const int ERROR_TTS_READ_RESPONSE_FAILED = 30206;
const int ERROR_BASE64_ENCODE_FAILED = 30207;
const int ERROR_REQID_NULL = 30208; //add liusy 20200210  Req 为空
const int ERROR_SEQID_NULL = 30209; //add liusy 20200210  Seq 为空
const int ERROR_PROPERTY_NULL = 30300;  //add liusy 20200210 参数为空
const int ERROR_SEQID_ORDER = 30301;    //add liusy 20200210   序列号错
const int ERROR_STOP = 30302;   //add liusy 20200210     客户端请求停止
const int ERROR_SESSION_TIMEOUT = 30303;    //add liusy 20200210     客户端请求停止

inline const char* get_err_msg(const int status);

inline const char* get_err_msg(const int status) {
    const char* message = "";

    switch (status) {
    case OK:
        message = "ok";
        break;

    case ERROR_PARAMETER:
        message = "invalid param";
        break;

    case ERROR_REQUEST_CNTL:
        message = "server err, invalid cntl";
        break;

    case ERROR_SERVER_BUSY:
        message = "server is busy now, call later";
        break;

    case ERROR_TEXT_TOO_SHORT:
        message = "text too short";
        break;

    case ERROR_TEXT_TOO_LONG:
        message = "text too long";
        break;

    case ERROR_SEQID_FOR_SESSION:
        message = "server err, error seqid";
        break;

    case ERROR_TTS_READ_RESPONSE_FAILED:
        message = "server err, read response failed";
        break;

    case ERROR_BASE64_ENCODE_FAILED:
        message = "server err, base64 encode failed";
        break;

    default:
        break;
    }

    return message;
}

inline int convert_errno_v0(const int status) {
    int err_no = -1;

    switch (status) {
    case OK:
        err_no = 0;
        break;

    case ERROR_REQUEST_CNTL:
        err_no = 1;
        break;

    case ERROR_SERVER_BUSY:
        err_no = 2;
        break;

    case ERROR_PARAMETER:
        err_no = 3;
        break;

    case ERROR_TEXT_TOO_SHORT:
        err_no = 4;
        break;

    case ERROR_TEXT_TOO_LONG:
        err_no = 5;
        break;

    case ERROR_SEQID_FOR_SESSION:
        err_no = 6;
        break;

    case ERROR_TTS_READ_RESPONSE_FAILED:
        err_no = 7;
        break;

    default:
        break;
    }

    return err_no;
}

const static char* ERROR_MSG[] = {
    "OK.",
    "server err.",
    "server is busy now, call later.",
    "params err.",
    "server err.",
    "server err.",
    "server err.",
};

#endif
